package com.qf.admin.controller.system;

import com.qf.admin.domain.form.UserForm;
import com.qf.common.domain.admin.dos.SysUser;
import com.qf.common.domain.admin.vo.UserPageVO;
import com.qf.admin.service.system.SysUserService;
import com.qf.common.aspect.ApiOperationLog;
import com.qf.common.core.domain.ResponseResult;
import com.qf.common.core.page.BaseController;
import com.qf.common.core.page.PageResult;
import com.qf.common.utils.ToolUtils;
import com.qf.common.domain.admin.vo.UserInfoVO;
import com.qf.jwt.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: sin
 * @Date: 2024/10/13 16:14
 * @Description:
 **/
@Api(tags = "04.用户接口")
@RestController
@RequestMapping("/admin/user")
@Slf4j
public class UserController extends BaseController {

    @Resource
    private SysUserService userService;

    /**
     * @Author: sin
     * @Date: 2024/10/13
     * @Description: 根据用户名、手机号、开启状态来获取分页用户信息
     **/
    @ApiOperation(value = "获取分页用户信息")
    @ApiOperationLog(description = "获取分页用户信息")
    @GetMapping("/page")
    public ResponseResult<PageResult> getUserPage(@RequestParam(value = "username", required = false) String username,
                                                  @RequestParam(value = "status", required = false) Integer status,
                                                  @RequestParam(value = "mobile", required = false) String mobile) {
        startPage();
        List<UserPageVO> userPageVoList = userService.getUserPage(username, mobile, status);
        return ResponseResult.success(getData(userPageVoList));
    }

    @ApiOperation(value = "更新用户信息")
    @ApiOperationLog(description = "更新用户信息")
    @PutMapping("/update")
    public ResponseResult<Boolean> Update(@RequestBody UserForm userForm) {
        return isOk(userService.updateUser(userForm));
    }

    @ApiOperation(value = "新增用户信息")
    @ApiOperationLog(description = "新增用户信息")
    @PostMapping("/save")
    public ResponseResult<Boolean> Save(@RequestBody UserForm userForm) {
        return isOk(userService.saveUser(userForm));
    }

    /**
     * @Author: sin
     * @Date: 2024/10/13
     * @Description: 根据ID删除用户,id通过,分割
     **/
    @ApiOperation(value = "删除用户")
    @ApiOperationLog(description = "删除用户")
    @DeleteMapping("/delById")
    public ResponseResult<Boolean> removeBatchByIds(@RequestParam(value = "ids", required = true) String ids) {
        List<Long> userIds = ToolUtils.getLongListByString(ids);
        return isOk(userService.removeBatchByIds(userIds));
    }

    @ApiOperation(value = "重置密码")
    @ApiOperationLog(description = "重置密码")
    @PatchMapping("/resetPassword")
    public ResponseResult<Boolean> resetPassword(@RequestBody SysUser user) {
        return isOk(userService.resetPassword(user));
    }

    @ApiOperation(value = "上传头像")
    @ApiOperationLog(description = "上传头像")
    @PostMapping("/uploadAvatar")
    public ResponseResult<Boolean> uploadAvatar(@RequestParam("avatar") MultipartFile avatar,
                                     @RequestParam("userId") Long userId) {
        return isOk(userService.uploadAvatar(avatar, userId));
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @ApiOperationLog(description = "获取当前登录用户信息")
    @GetMapping("/getCurrentUser")
    public ResponseResult<UserInfoVO> getUserInfo() {
        SysUser user = SecurityUtils.getUserInfo();
        UserInfoVO userInfoVo = new UserInfoVO(user.getId(), user.getUsername(), user.getNickname(), user.getAvatar(), user.getEmail(), user.getMobile(), user.getGender(), user.getRoles().get(0).getName(), user.getContent(), user.getCreateTime());
        return ResponseResult.success(userInfoVo);
    }
}

